﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace AIthin.Model.Body
{
    class Retina
    {
        /// <summary>
        /// High = Width = Size
        /// </summary>
        public int Size { get; internal set; }
        public int xPosition { get; set;}
        public int yPosition { get; set;}

        public Retina(int size)
        {
            Size = size;
            xPosition = 9;
            yPosition = 19;
        }

        public void Draw(Graphics g)
        {
      
            g.DrawRectangle(Pens.Red, xPosition, yPosition, Size, Size);
        }
        
        public void up_down(int dy, int ymax)
        {
            yPosition += dy;
            if (yPosition + Size > ymax) yPosition = ymax - Size;
            if (yPosition < 0) yPosition = 0;
        }

        public void left_right(int dx, int xmax)
        {
            xPosition += dx;
            if (xPosition + Size > xmax) xPosition = xmax - Size;
            if (xPosition < 0) xPosition = 0;
        }

        public short[,] ReadBitmap( Bitmap bm)
        {

            int Width = Size;
            int Height = Size;

            short[,] Input = new short[Width,Height];

            int bm_H = bm.Height;


            for (int i = 0; i < Width; i++)
                for (int j = 0; j < Height; j++)
                {
                    //RES speed  
                    //TODO now Bitmap Size is 2*Hight, Hight
                    if (xPosition + i < bm_H & yPosition + j < bm_H)
                         // !!! black is 1
                        Input[i, j] = Convert.ToInt16((ColorToShort(bm.GetPixel(xPosition + i, yPosition + j)) > 0) ? 0 : 1);
                    else
                        Input[i, j] = 0;
                }

            return Input;
        }

        private short ColorToShort(Color inColor)
        {
            //RES speed
            return Convert.ToInt16((inColor.R + inColor.G + inColor.B) / 3.0f);
        }
    }
}
